package helper;

import model.BinaryqDAO;
import model.ContinuousqDAO;
import model.LikertqDAO;
import model.MultipleChoiceqDAO;
import model.NumericqDAO;
import model.QuestionDAO;
import model.SocioCognitiveqDAO;
import model.SurveyDAO;

import org.mybeans.dao.DAOException;

import databeans.MultipleChoiceq;
import databeans.Question;

public class DeleteCascade {
	
	// Delete a Question with specified qid.
	public static void deleteQuestion(int qid, QuestionDAO questionDAO, BinaryqDAO binaryqDAO, NumericqDAO numericqDAO,
			LikertqDAO likertqDAO, MultipleChoiceqDAO mcDAO, ContinuousqDAO continuousqDAO, SocioCognitiveqDAO scDAO) throws DAOException {
		Question question = questionDAO.lookup(qid);
		
		// First delete all associated data on other tables with this qid
		if (question.getType() == QuestionType.BINARY)
			binaryqDAO.delete(qid);
		else if (question.getType() == QuestionType.NUMERIC)
			numericqDAO.delete(qid);
		else if (question.getType() == QuestionType.LIKERT)
			likertqDAO.delete(qid);
		else if (question.getType() == QuestionType.MULTIPLE_CHOICE ||
				question.getType() == QuestionType.MULTIPLE_SELECTION ||
				question.getType() == QuestionType.RANK_ORDER ||
				question.getType() == QuestionType.NETWORK_WEIGHTED ||
				question.getType() == QuestionType.NETWORK_RANK_ORDER) {
			MultipleChoiceq[] mclist = mcDAO.getChoicesByQid(qid);
			for (int i = 0; i < mclist.length; i++)
				mcDAO.delete(qid, mclist[i].getPos());
		}
		else if (question.getType() == QuestionType.CONTINUOUS ||
				question.getType() == QuestionType.NETWORK_WEIGHTED_CONTINUOUS)
			continuousqDAO.delete(qid);
		else if (question.getType() == QuestionType.NETWORK_SOCIO_COGNITIVE)
			scDAO.delete(qid);
		
		questionDAO.delete(qid);
	}
	
	// Delete a Survey with specified sid.
	public static void deleteSurvey(int sid, SurveyDAO surveyDAO, QuestionDAO questionDAO, BinaryqDAO binaryqDAO, NumericqDAO numericqDAO,
			LikertqDAO likertqDAO, MultipleChoiceqDAO mcDAO, ContinuousqDAO continuousqDAO, SocioCognitiveqDAO scDAO) throws DAOException {
		
		// First delete all the questions in the survey
		Question[] questionlist = questionDAO.getQuestionBySid(sid);
		for (int i = 0; i < questionlist.length; i++)
			deleteQuestion(questionlist[i].getQid(), questionDAO, binaryqDAO, numericqDAO, likertqDAO, mcDAO, continuousqDAO, scDAO);
		
		surveyDAO.delete(sid);
	}
}
